import React from 'react';
import { Modal } from 'antd-mobile';
import { Prompt } from 'react-router-dom';
import { history } from 'umi';

interface LeavePromptProps {
  title?: string;
}
interface LeavePromptState {
  show: boolean;
  confirmShow: boolean;
}
class LeavePrompt extends React.PureComponent<LeavePromptProps, LeavePromptState> {
  static defaultProps = {
    title: '确定要返回？',
  };

  constructor(props) {
    super(props);
    this.state = {
      show: true,
      confirmShow: false,
    };
  }

  showConfirm() {
    this.setState({ confirmShow: true });
  }

  handleGo() {
    this.setState({ show: false }, async () => {
      // const data = await orderList();
      // if(data.code === 0) {
      //   history.goBack();
      // }
      history.goBack();
    });
  }

  render() {
    const { show, confirmShow } = this.state;
    const { title } = this.props;
    return (
      <div>
        {show && (
          <Prompt
            message={(location, action) => {
              if (action === 'POP') {
                this.showConfirm();
                return false;
              }
              return true;
            }}
          />
        )}
        <Modal
          title={title}
          transparent
          visible={confirmShow}
          footer={[
            {
              text: '取消',
              onPress: () => {
                this.setState({
                  confirmShow: false,
                });
              },
            },
            {
              text: '确定',
              onPress: () => {
                this.handleGo();
              },
            },
          ]}
        />
      </div>
    );
  }
}

export default LeavePrompt;
